﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Admin_Asp_UserControls_ModelEditHandler : System.Web.UI.UserControl
{
    protected ModelItem m_item = null;
    protected List<ModelItem> m_items;    

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    /// <summary>
    /// Loads the usercontrol with data from an item
    /// </summary>
    /// <param name="a_item">The selected item</param>
    /// <param name="a_items">The item list</param>
    /// <param name="a_updateCallback">Update functions to be run when user clicks on "Save Changes"</param>
    /// <param name="a_deleteCallback">The delete function to remove the model</param>
    public void LoadItem(ModelItem a_item, List<ModelItem> a_items )
    {
        m_item = a_item;
        m_items = a_items;
        
        x_edit_modelPath.Text = m_item.m_path;
        x_edit_name.Text = m_item.m_name;
        x_edit_thumbnail.Text = m_item.m_thumbnailPath;

        if (Request.QueryString[Strings.Query_Control] != null)
        {
            string f_control = Request.QueryString[Strings.Query_Control];

            if (f_control == Strings.Query_Control_Shader)
            {
                x_shaderSelection.Visible = true;
            }
        }

        if (Session[Strings.Session_Name] != null)
        {
            x_edit_name.Text = Session[Strings.Session_Name] as string;
        }
        if (Session[Strings.Session_Thumbnail] != null)
        {
            x_edit_thumbnail.Text = Session[Strings.Session_Thumbnail] as string;
        }

        if (Session[Strings.Session_TextureChanges] != null)
        {
            Dictionary<Texture.Type, string> f_changes = Session[Strings.Session_TextureChanges] as Dictionary<Texture.Type, string>;

            if (f_changes.ContainsKey(Texture.Type.normal))
            {
                x_normalmap_label.Text = f_changes[Texture.Type.normal];
            }
            else
            {
                x_normalmap_label.Text = ((m_item.GetTexture(Texture.Type.normal)) != null) ? m_item.GetTexture(Texture.Type.normal).m_path : "";
            }
            if (f_changes.ContainsKey(Texture.Type.diffuse))
            {
                x_diffusemap_label.Text = f_changes[Texture.Type.diffuse];
            }
            else
            {
                x_diffusemap_label.Text = ((m_item.GetTexture(Texture.Type.diffuse)) != null) ? m_item.GetTexture(Texture.Type.diffuse).m_path : "";
            }
        }
        else
        {
            x_normalmap_label.Text = ((m_item.GetTexture(Texture.Type.normal)) != null) ? m_item.GetTexture(Texture.Type.normal).m_path : "";

            x_diffusemap_label.Text = ((m_item.GetTexture(Texture.Type.diffuse)) != null) ? m_item.GetTexture(Texture.Type.diffuse).m_path : "";
        }

        x_edit_thumbnail_image.ImageUrl = "~/" + x_edit_thumbnail.Text;
        x_diffusemap_image.ImageUrl = "~/" + x_diffusemap_label.Text;
        x_normalmap_image.ImageUrl = "~/" + x_normalmap_label.Text;

        //If there is no data about shaders in the session then add it
        if (Session[Strings.Session_Shaders] == null)
        {
            List<string> f_tempShaderList = new List<string>();    
            //Copy the data since otherwise it'd write directly to the array
            for (int i = 0; i < m_item.m_shaders.Count; i++)
            {
                f_tempShaderList.Add(m_item.m_shaders[i]);
            }

            Session[Strings.Session_Shaders] = f_tempShaderList;
        }

        List<string> f_shadersList = Session[Strings.Session_Shaders] as List<String>;

        foreach (string shader in f_shadersList)
        {
            x_edit_shadersLabel.Text += shader + " ";
        }
    }

    protected void ThumbnailChange(object sender, EventArgs e)
    {
        ShowThumbnailSelection();
    }

    protected void normalMap_change(object sender, EventArgs e)
    {
        Session[Strings.Session_CurrentTextureChange] = Texture.Type.normal; 
        ShowTextureSelection();                   
    }

    protected void diffuseMap_change(object sender, EventArgs e)
    {
        Session[Strings.Session_CurrentTextureChange] = Texture.Type.diffuse;
        ShowTextureSelection();       
    }

    protected void ShadersChange(object sender, EventArgs e)
    {       
        ShowShadersSelection();
    }

    //When user has clicked OK on the confirmation popup to delete
    protected void x_edit_delete_Click(object sender, EventArgs e)
    {       
        //Remove Item

        //Save List        

        Response.Redirect(Paths.admin_edit_model);
    }
    //When user clicks on save button
    protected void x_edit_save_Click(object sender, EventArgs e)
    {
        string f_errorMsg = "";
        //Copy item so reference isn't copied when making changes
        ModelItem f_item = m_item.Clone();

        //Set all the changes
        f_item.EditName(x_edit_name.Text);
        f_item.EditThumbnailPath(x_edit_thumbnail.Text);
            //Get Shaders from a " " split
        List<string> f_shadersList = Session[Strings.Session_Shaders] as List<String>;
        f_item.EditShaders(f_shadersList);
                //Do stuff
            //Check each texture
                //Diffuse
        f_item.EditTexture(Texture.Type.diffuse, x_diffusemap_label.Text);
                //Normal
        f_item.EditTexture(Texture.Type.normal, x_normalmap_label.Text);

        //Validate new item     
        if (ValidatorFactory.StrictValidation( f_item) )
        {
            //Save List (It will handle copying values from f_item and making a backup e.t.c.)
            if (ModelItemHandler.UpdateModelItemsList(m_items, f_item, m_item))
            {
                //If list saves succesfully,  remove Session variables
                Session.Remove(Strings.Session_TextureChanges);
                Session.Remove(Strings.Session_Shaders);
                Session.Remove(Strings.Session_Thumbnail);
                Session.Remove(Strings.Session_Name);

                f_errorMsg = "Successfully updated the model";
                x_edit_errorMsg.CssClass = "validationSuccess";
            }
            else
            {
                f_errorMsg = "Something went wrong when saving model items to file";
            }
        }
        else
        {
            f_errorMsg = ValidatorFactory.GetErrorMsg();
        }       
         
        //Error messages incase it fails anywhere
        if (f_errorMsg != "")
        {
            x_edit_errorMsg.Text = f_errorMsg;
            x_edit_errorMsg.Visible = true;
        }
    }

    private void ShowThumbnailSelection()
    {
        x_thumbnailSelection.Visible = true;
        x_textureSelection.Visible = false;
        x_shaderSelection.Visible = false;
    }

    private void ShowShadersSelection()
    {
        x_thumbnailSelection.Visible = false;
        x_textureSelection.Visible = false;
        x_shaderSelection.Visible = true;
    }

    private void ShowTextureSelection()
    {
        x_thumbnailSelection.Visible = false;
        x_textureSelection.Visible = true;        
        x_shaderSelection.Visible = false;
    }
    protected void NameChanged(object sender, EventArgs e)
    {
        Session[Strings.Session_Name] = x_edit_name.Text;
    }
}
